package com.lanou3g.dllo.baidumusic.mine.localmusic;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import com.lanou3g.dllo.baidumusic.database.SingleLiteOrm;
import com.litesuits.orm.db.assit.QueryBuilder;

/**
 * Created by dllo on 16/7/7.
 */

public class LocalMusic {
    public static void query(Context context) {
        // 创建一个Cursor
        Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if (cursor != null) {
            // 创建Model对象
            LocalMusicBean localMusicBean = new LocalMusicBean();
            // 循环读取,实例化Model对象
            while (cursor.moveToNext()) {
                // 扫描本地文件,得到歌曲的相关信息
                String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String author = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                // 设置值到Model的封装类
                localMusicBean.setTitle(songName);
                localMusicBean.setAuthor(author);
                localMusicBean.setData(data);

                QueryBuilder<LocalMusicBean> queryBuilder = new QueryBuilder<>(LocalMusicBean.class);
                queryBuilder.whereEquals("title", songName);
                if (SingleLiteOrm.getSingleLiteOrm().getLiteOrm().query(queryBuilder).size() == 0) {
                    SingleLiteOrm.getSingleLiteOrm().getLiteOrm().insert(new LocalMusicBean(songName, author, data));
                }

            }

        }

    }
}
